Bypass authentication for anonymous static resources - #2273
Open
sdelamo wants to merge 9 commits into
Open
Conversation
Add an opt-in security filter setting that skips authentication fetchers for unrouted GET and HEAD requests resolving to static resources with anonymous access. Provide a replaceable bypass strategy, preserve authentication for routed and protected resources, and cover enabled, disabled, and custom implementations with tests. Close: #2267
Contributor
There was a problem hiding this comment.
Pull request overview
Adds an opt-in mechanism in the security module to skip running AuthenticationFetcher implementations for unrouted GET/HEAD requests that resolve to static resources and are configured for anonymous access, addressing issue #2267’s performance concern for static assets when cookies are present.
Changes:
- Introduces
StaticResourceAuthenticationBypassSPI plus a default implementation that detects unrouted static resources and consults the intercept-url-map for anonymous access. - Adds
micronaut.security.filter.static-resource-authentication-bypassconfiguration toggle and wires it intoSecurityFilter. - Adds tests covering enabled/disabled behavior and custom SPI replacement, plus test static resources.
Reviewed changes
Copilot reviewed 12 out of 12 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| security/src/main/java/io/micronaut/security/filters/SecurityFilter.java | Adds bypass hook before authentication fetchers; introduces new injected constructor and deprecates old one. |
| security/src/main/java/io/micronaut/security/filters/SecurityFilterConfiguration.java | Adds new config flag accessor for the bypass feature. |
| security/src/main/java/io/micronaut/security/filters/SecurityFilterConfigurationProperties.java | Implements/binds the new bypass configuration property. |
| security/src/main/java/io/micronaut/security/filters/StaticResourceAuthenticationBypass.java | New SPI interface for pluggable bypass decision. |
| security/src/main/java/io/micronaut/security/filters/DefaultStaticResourceAuthenticationBypass.java | Default bypass strategy using route-match absence + static resource resolution + intercept-url-map anonymous access. |
| security/src/main/java/io/micronaut/security/rules/InterceptUrlMapRule.java | Refactors pattern matching into findPattern(...) for reuse. |
| security/src/main/java/io/micronaut/security/rules/ConfigurationInterceptUrlMapRule.java | Adds internal helper isAnonymous(request) for bypass decision. |
| security/src/test/java/io/micronaut/security/filters/StaticResourceAuthenticationBypassTest.java | Tests bypass-enabled behavior (static anonymous vs routed vs protected static). |
| security/src/test/java/io/micronaut/security/filters/StaticResourceAuthenticationBypassDisabledTest.java | Tests that authentication is still resolved when bypass is disabled. |
| security/src/test/java/io/micronaut/security/filters/StaticResourceAuthenticationBypassCustomImplementationTest.java | Tests that a custom SPI bean replaces the default implementation. |
| security/src/test/resources/static-resource-authentication-bypass/asset.txt | Test static resource content for anonymous access case. |
| security/src/test/resources/static-resource-authentication-bypass/protected.txt | Test static resource content for protected static resource case. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
- Do not close the RouteMatch in DefaultStaticResourceAuthenticationBypass. It is only inspected here and is executed afterwards; close() is a no-op today but closing a route match we do not consume is a latent trap. - Add StaticResourceAuthenticationBypassRouteTakesPrecedenceTest, which maps a controller onto a path that also resolves to a static resource. The existing routed test used a path outside the static resource mapping, so the route-precedence guard was not actually covered. - Exercise the custom StaticResourceAuthenticationBypass through the filter instead of only asserting the injected bean type, including the case where the custom implementation declines to bypass. - Document the feature in the guide, including the requirement for an explicit isAnonymous() intercept URL map entry and the fact that security rules are still evaluated, with a null authentication. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Add an opt-in security filter setting that skips authentication fetchers for unrouted GET and HEAD requests resolving to static resources with anonymous access.
Provide a replaceable bypass strategy, preserve authentication for routed and protected resources, and cover enabled, disabled, and custom implementations with tests.
Close: #2267